{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[演示分形几何（fractal geometry）](../portfolio/fractals.slides.html)对比Python和Julia的性能\n",
    "\n",
    "<!-- TEASER_END -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def julia(z, c, maxiter=200):\n",
    "    for n in xrange(maxiter):\n",
    "        if abs(z) > 4:\n",
    "            return n-1\n",
    "        \n",
    "        z = z*z + c\n",
    "    \n",
    "    return maxiter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 10.6 s, sys: 59.6 ms, total: 10.6 s\n",
      "Wall time: 10.6 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "for i in np.r_[1:-1:-.002]:\n",
    "    for r in np.r_[-1.5:1.5:0.002]:\n",
    "        julia(complex(r,i), complex(-.06, .67))\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Julia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# julia set\n",
    "# (the familiar mandelbrot set is obtained by setting c==z initially)\n",
    "# function julia(z, c; maxiter=1000)\n",
    "#     for n = 1:maxiter\n",
    "#         if abs2(z) > 4\n",
    "#             return n-1\n",
    "#         end\n",
    "#         z = z*z + c\n",
    "#     end\n",
    "#     return maxiter\n",
    "# end"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#@time m = [ uint8(julia(complex(r,i), complex(-.06,.67))) for i=1:-.002:-1, r=-1.5:.002:1.5 ];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#X, Y = np.ogrid[1:-1:-.002, -1.5:1.5:.002]\n",
    "#m = [[ int(julia(complex(r,i), complex(-.06,.67))) for i in np.arange(1, 1,-.002)] \n",
    "#     for r in np.arange(-1.5, 1.5, 0.002)];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# elapsed time: 0.247828677 seconds (1502560 bytes allocated)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Time cost"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "42.771482817543344"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "10.6 / 0.247828677"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Julia 0.4.2",
   "language": "julia",
   "name": "julia-0.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
